// pages/autWay/jsz.js
const app = getApp()
import {
  urls
} from '../../api/urls'
import Toast from '../../dist/toast/toast';
import {
  authentication
} from '../../api/request'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    fileList: [],
    identity_room_code: '', //门牌号
    identity_Url: '', //身份证照片上传地址
    identity_name: '', //身份证姓名
    identity_mobile: '', //联系方式

    identity_id: '', //证号
    identity_addr: '', //身份证地址，
    identity_carClass: '', //驾照类型
    identity_officialSeal: '', //颁发机构
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(app.globalData)
  },

  //业主用户协议
  xieyi() {
    wx.navigateTo({
      url: '/pages/xq/index?broadcast_id=0000'
    })
  },


  //实名认证
  authentication() {
    let that = this
    var data = {
      para: [app.globalData.cur_property_code,
        app.globalData.cur_my_openid,
        that.data.identity_name
      ]
    }
    authentication(data).then(res => {
      console.log('----authentication------', res)
      if (res.data[0].ERRORCODE == '1') {
        console.log('实名认证成功', res.data[0].ROOM_CODE)
        that.setData({
          identity_room_code: res.data[0].PROPERTY_NAME + '(' + res.data[0].ROOM_CODE + ')',
          identity_mobile: res.data[0].MOBILE
        })

        console.log('------驾驶证[' + that.data.identity_name + ']已通过实名认证，转主页面-----')
        wx.setStorageSync('certification_status', true)
        //更新全局变量
        app.globalData.phoneNumber = res.data[0].MOBILE
        app.globalData.cur_room_code = res.data[0].ROOM_CODE
        app.globalData.cur_my_name = res.data[0].ROOM_NAME
        app.globalData.cur_property_code = res.data[0].PROPERTY_CODE
        app.globalData.cur_property_name = res.data[0].PROPERTY_NAME
        app.globalData.cur_street_name = res.data[0].STREET_NAME
        wx.setStorageSync('openid', app.globalData.cur_my_openid)

        wx.showModal({
          title: '驾驶证实名认证成功',
          confirmText: "确定",
          showCancel: false, //是否显示取消按钮
          content: '你的微信号已实名认证到' + app.globalData.cur_property_name + '(' + app.globalData.cur_room_code + ')',
        })

        Toast.success('实名认证成功' + app.globalData.cur_property_name + '(' + app.globalData.cur_room_code + ')');

      } else {
        console.log('-----驾驶证实名认证失败------')
        wx.showModal({
          title: '驾驶证实名认证失败',
          confirmText: "确定",
          showCancel: false, //是否显示取消按钮
          content: '请尝试改用其它方式进行实名认证',
        })

      }
    })
  },


  nextToPage() {
    wx.navigateBack({

    })
  },

  jiashizheng() {
    this.photo("jiashizheng")
  },

  //拍照或者从相册选择要识别的照片
  photo(type) {
    let that = this
    var typess = ''
    wx.showActionSheet({
      itemList: ['从手机相册选择', '拍照'],
      success: function (res) {
        console.log(res.tapIndex)
        if (res.tapIndex == '0') typess = 'album'
        if (res.tapIndex == '1') typess = 'camera'
        wx.chooseImage({
          count: 1,
          sizeType: ['original', 'compressed'],
          sourceType: [typess],
          success(res) {
            // tempFilePath可以作为img标签的src属性显示图片
            let imgUrl = res.tempFilePaths[0];
            that.uploadImg(type, imgUrl)
            wx.showLoading({
              title: '认证中...'
            })
          }
        })
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },

  // 上传图片到云存储
  uploadImg(type, imgUrl) {
    let that = this
    wx.cloud.uploadFile({
      cloudPath: 'ocr/' + app.globalData.cur_property_code+'-'+app.globalData.cur_my_openid + '.png',
      filePath: imgUrl, // 文件路径
      success: res => {
        console.log("上传成功！", res.fileID)
        that.getImgUrl(type, res.fileID)
        wx.hideLoading()
      },
      fail: err => {
        console.log("上传失败", err)
      }
    })
  },

  //获取云存储里的图片url
  getImgUrl(type, imgUrl) {
    let that = this
    wx.cloud.getTempFileURL({
      fileList: [imgUrl],
      success: res => {
        let imgUrl = res.fileList[0].tempFileURL
        console.log("获取图片url成功", imgUrl)
        that.setData({
          identity_Url: imgUrl
        })
        that.shibie(type, imgUrl)
        wx.showLoading({
          title: '确认中...'
        })
      },
      fail: err => {
        console.log("获取图片url失败", err)
      }
    })
  },

  //调用云函数，实现OCR识别
  shibie(type, imgUrl) {
    let that = this
    wx.cloud.callFunction({
      name: "orc",
      data: {
        type: type,
        imgUrl: imgUrl
      },
      success(res) {
        console.log("证件识别完成！", res)
        wx.hideLoading()
        var showInfoData = res.result
        that.setData({
          identity_name: res.result.name,
          identity_addr: res.result.address,
          identity_carClass: res.result.carClass, //驾照类型
          identity_id: res.result.idNum, //驾照编号
          identity_officialSeal: res.result.officialSeal, //颁发机构

          /*
          address: "重庆市江北区洋河中路35号2幢6-6"
          birthDate: "1970-04-19"
          carClass: "C1"
          errCode: 0
          errMsg: "openapi.ocr.driverLicense:ok"
          idNum: "512902197004190469"
          issueDate: "2001-12-29"
          name: "王永勤"
          nationality: "中国"
          officialSeal: "重庆市公安局交通管理局"
          sex: "女"
          validFrom: "20131229"
          validTo: "20231229"
          */

        })
        that.authentication();
      },
      fail(res) {
        wx.hideLoading()
        Toast.fail('识别失败');
      },
    })
  }
})